package com.yuhanginfo.modules.web.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.service.WebCrudService;
import com.yuhanginfo.modules.web.entity.WebAddress;
import com.yuhanginfo.modules.web.dao.AddressDao;

/**
 * 地址管理Service
 * @author lvls
 * @version 2017-10-24
 */
@Service
@Transactional(readOnly = true)
public class AddressService extends WebCrudService<AddressDao, WebAddress> {

	@Autowired
	private AddressDao addressDao;
	
	public WebAddress get(String id) {
		return super.get(id);
	}
	
	public List<WebAddress> findList(WebAddress address) {
		return super.findList(address);
	}
	
	public Page<WebAddress> findPage(Page<WebAddress> page, WebAddress address) {
		return super.findPage(page, address);
	}
	
	@Transactional(readOnly = false)
	public void save(WebAddress address) {
		super.save(address);
	}
	
	@Transactional(readOnly = false)
	public void delete(WebAddress address) {
		super.delete(address);
	}
	
	/**
	 * 根据用户id查询该用户的收货地址
	 * @param userId
	 * @return
	 */
	public List<WebAddress> selectByUserId(Integer userId){
		return addressDao.selectByUserId(userId);
	}
	
	@Transactional(readOnly = false)
	public int saveAddress(WebAddress address) {
		if (address.getIsNewRecord()){
			address.preInsert();
			return addressDao.saveAddress(address);
		}else{
			address.preUpdate();
			return addressDao.updateAddress(address);
		}
		
	}

	public WebAddress getDefAddress(WebAddress address) {
		return addressDao.getDefAddress(address);
	}

	public List<WebAddress> selectUserId(String string) {
		
		return addressDao.selectUserId(string);
	}
    
	@Transactional(readOnly = false)
	public void updateDeauft(WebAddress address) {
		
		addressDao.updateDeauft(address);
	}
	
	@Transactional(readOnly = false)
	public void deleteAddress(String id) {
		addressDao.deleteAddress(id);
		
	}

	@Transactional(readOnly = false)
	public void saveMyAddress(WebAddress address) {
		addressDao.saveMyAddress(address);
		
	}

	@Transactional(readOnly = false)
	public void updateMyAddress(WebAddress address) {
		addressDao.updateMyAddress(address);
		
	}

	public WebAddress getNewAddress(String string) {
		return addressDao.getNewAddress(string);
	}

	public List<WebAddress> selectPhoneUserId(String id) {
		
		return addressDao.selectPhoneUserId(id);
	}

	public List<WebAddress> selectPhoneList(Map<String, Object> map) {
		
		return addressDao.selectPhoneList(map);
	}

	public WebAddress selectByAddressId(String receiveAreaId) {
		
		return addressDao.selectByAddressId(receiveAreaId);
	}
}